<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
use Common\Model\LinksModel;

class LinksController extends  CommonController{

	public function index(){
		$model = new LinksModel();
		$data = $model->getList();
		$url = U("admin/links/index");
		$page = PageShow($data['count'],$url);
		$this->assign('data',$data)->assign('page',$page);
		$this->display();
	}


	public function add(){
		if(IS_POST){
			$links = new LinksModel();
			if(!$links->addLink()){
				$this->error($links->getError());
			}
			$this->success('添加成功',U("admin/links/index"));
		}
		$this->display();
	}

	public function edit(){
		$id = I('id');
		$links = new LinksModel();
		if(IS_POST){
			if(!$links->editLink()){
				$this->error($links->getError());
			}
			$this->success('添加成功',U("admin/links/index"));
		}else{
			$data = $links->getLinkById($id);
			$this->assign('data',$data);
		}
		$this->display();
	}

	public function del(){
		$id = I('id');
		$res = M('links')->delete($id);
		if($res){
			$this->success("删除成功",U("admin/links/index"));
		}else{
			$this->error("删除失败,请重试");
		}
	}
}
